//
//  DestinationDetailViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "DestinationDetailViewController.h"
#import "DestinationDatabaseManager.h"

@interface DestinationDetailViewController ()<UIScrollViewDelegate, MONActivityIndicatorViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *titleImage;
@property (nonatomic, strong) MONActivityIndicatorView *indicatorView;
@property (nonatomic, strong) UIButton *collectButton;

@end

@implementation DestinationDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addIndicatorView];
       [self addSubViews];
}

- (void)addSubViews
{
    [self setNavigationBar];
    [self addScrollView];
}

- (void)viewDidAppear:(BOOL)animated
{
   DestinationScenicModel *newModel = [[DestinationDatabaseManager shareManager] selectModelByName:self.model.name];
    if ([newModel.name isEqualToString:self.model.name]) {
        self.collectButton.selected = YES;
    } else {
        self.collectButton.selected = NO;
    }
}

- (void)setNavigationBar
{
 self.navigationItem.title = @"目的地详情";
    
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
    self.collectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.collectButton.frame = CGRectMake(0, 0, 32, 32);
    [self.collectButton setImage:[UIImage imageNamed:@"detailCollect.png"] forState:(UIControlStateNormal)];
    [self.collectButton setImage:[UIImage imageNamed:@"collectSelect.png"] forState:(UIControlStateSelected)];
    [self.collectButton addTarget:self action:@selector(actionRightButton:) forControlEvents:(UIControlEventTouchUpInside)];
    rightButton.customView = self.collectButton;
    rightButton.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightButton;
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backNew.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionLeft:)];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)actionLeft:(UIBarButtonItem *)leftButton
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)addScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.scrollView.delegate = self;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.contentInset = UIEdgeInsetsMake(210, 0, 0, 0);
    self.titleImage = [[UIImageView alloc] init];
    self.titleImage.contentMode = UIViewContentModeScaleAspectFill;
    self.titleImage.layer.masksToBounds = YES;
    self.titleImage.frame = CGRectMake(0, -210.0f, kScreenWidth, 210);
    [self.titleImage sd_setImageWithURL:[NSURL URLWithString:self.model.cover] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];;
    [self.scrollView addSubview:self.titleImage];
    
    // 标题的lable
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, kScreenWidth - 17, 30)];
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.font = [UIFont systemFontOfSize:23];
    titleLable.text = self.model.name;
    [self.scrollView addSubview:titleLable];
    
    //介绍的lable
    UILabel *recommendLable = [[UILabel alloc] initWithFrame:CGRectMake(10, titleLable.bottom + 15, kScreenWidth - 17, 25)];
    recommendLable.text = self.model.recommended_reason;
    recommendLable.numberOfLines = 0;
    recommendLable.font = [UIFont systemFontOfSize:14];
    recommendLable.textAlignment = NSTextAlignmentCenter;
    CGRect newFrame = recommendLable.frame;
    newFrame.size.height = [DestinationDetailViewController cellHeightWithModel:self.model.recommended_reason font:14];
    recommendLable.frame = newFrame;
    [self.scrollView addSubview:recommendLable];
    
    // 图片
    UIImageView *detailImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 255) / 2, recommendLable.bottom + 20, 255, 50)];
    detailImageView.image = [UIImage imageNamed:@"DetailTitle.png"];
    
    // 图片上的label
    UILabel *imageLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 17, 255, 16)];
    imageLable.text = @"        具       体       详       情";
    imageLable.backgroundColor = [UIColor whiteColor];
    [detailImageView addSubview:imageLable];
    [self.scrollView addSubview:detailImageView];
    
    // 概括的lable
    UILabel *descripLable = [[UILabel alloc] initWithFrame:CGRectMake(10, detailImageView.bottom + 20, kScreenWidth - 20, 30)];
    descripLable.font = [UIFont systemFontOfSize:22];
    descripLable.text = @"概括:";
    
    [self.scrollView addSubview:descripLable];
    // 概括详细
    UILabel *descripeDetailLable = [[UILabel alloc] initWithFrame:CGRectMake(10, descripLable.bottom, kScreenWidth - 17, 50)];
    descripeDetailLable.font = [UIFont systemFontOfSize:14];
    descripeDetailLable.text = self.model.descrip;
    descripeDetailLable.numberOfLines = 0;
    CGRect newFrame1 = descripeDetailLable.frame;
    newFrame1.size.height = [DestinationDetailViewController cellHeightWithModel:self.model.descrip font:14];
    descripeDetailLable.frame = newFrame1;
    [self.scrollView addSubview:descripeDetailLable];
    
    // 地址
    UILabel *addressLable = [[UILabel alloc] initWithFrame:CGRectMake(10, descripeDetailLable.bottom + 20, kScreenWidth - 17, 30)];
    addressLable.font = [UIFont systemFontOfSize:22];
    addressLable.text = @"地址:";
    [self.scrollView addSubview:addressLable];
    
    // 地址详细
    UILabel *addressDetailLable = [[UILabel alloc] initWithFrame:CGRectMake(10, addressLable.bottom, kScreenWidth - 17, 50)];
    addressDetailLable.font = [UIFont systemFontOfSize:14];
    addressDetailLable.text = self.model.address;
    addressDetailLable.numberOfLines = 0;
    CGRect newFrame2 = addressDetailLable.frame;
    newFrame2.size.height = [DestinationDetailViewController cellHeightWithModel:self.model.address font:14];
    addressDetailLable.frame = newFrame2;
    
    [self.scrollView addSubview:addressDetailLable];
   
    // 到达方式:
    UILabel *arrivalLable = [[UILabel alloc] initWithFrame:CGRectMake(10, addressDetailLable.bottom + 20, kScreenWidth - 17, 30)];
    arrivalLable.font = [UIFont systemFontOfSize:22];
    arrivalLable.text = @"到达方式:";
    [self.scrollView addSubview:arrivalLable];
    // 到达方式详细
    UILabel *arrivalDeatilLable = [[UILabel alloc] initWithFrame:CGRectMake(10, arrivalLable.bottom, kScreenWidth - 17, 50)];
    arrivalDeatilLable.font = [UIFont systemFontOfSize:14];
    arrivalDeatilLable.numberOfLines = 0;
    arrivalDeatilLable.text = self.model.arrival_type;
    CGRect newFrame3 = arrivalDeatilLable.frame;
    newFrame3.size.height = [DestinationDetailViewController cellHeightWithModel:self.model.arrival_type font:14];
    arrivalDeatilLable.frame = newFrame3;
    [self.scrollView addSubview:arrivalDeatilLable];

    // 开放时间
    UILabel *openLable = [[UILabel alloc] initWithFrame:CGRectMake(10, arrivalDeatilLable.bottom + 20, kScreenWidth - 17, 30)];
   openLable.font = [UIFont systemFontOfSize:22];
    openLable.text = @"开放时间:";
    [self.scrollView addSubview:openLable];
    // 开放时间详细
    UILabel *openDeatilLable = [[UILabel alloc] initWithFrame:CGRectMake(10, openLable.bottom, kScreenWidth - 17, 50)];
    openDeatilLable.font = [UIFont systemFontOfSize:14];
    openDeatilLable.numberOfLines = 0;
    openDeatilLable.text = self.model.opening_time;
    CGRect newFrame4 = openDeatilLable.frame;
    newFrame4.size.height = [DestinationDetailViewController cellHeightWithModel:self.model.opening_time font:14];
    openDeatilLable.frame = newFrame4;
    [self.scrollView addSubview:openDeatilLable];
    
    // 门票价格
    UILabel *feeLable = [[UILabel alloc] initWithFrame:CGRectMake(10, openDeatilLable.bottom + 20, kScreenWidth - 17, 30)];
    feeLable.font = [UIFont systemFontOfSize:22];
    feeLable.text = @"门票价格:";
    [self.scrollView addSubview:feeLable];
    // 门票价格详细
    UILabel *feeDeatilLable = [[UILabel alloc] initWithFrame:CGRectMake(10, feeLable.bottom, kScreenWidth - 17, 50)];
    feeDeatilLable.font = [UIFont systemFontOfSize:14];
    feeDeatilLable.numberOfLines = 0;
    feeDeatilLable.text = self.model.fee;
    CGRect newFrame5 = feeDeatilLable.frame;
    newFrame5.size.height = [DestinationDetailViewController cellHeightWithModel:self.model.fee font:14];;
    feeDeatilLable.frame = newFrame5;

    [self.scrollView addSubview:feeDeatilLable];

    self.scrollView.contentSize = CGSizeMake(kScreenWidth, feeDeatilLable.bottom + 80);
    
    [self.view addSubview:self.scrollView];
    [self.indicatorView stopAnimating];
}

#pragma mark - 添加加载等待动画
- (void)addIndicatorView
{
    _indicatorView = [[MONActivityIndicatorView alloc] init];
    _indicatorView.center = self.view.center;
    _indicatorView.delegate = self;
    
    _indicatorView.numberOfCircles = 5; // 圆点个数
    _indicatorView.internalSpacing = 5; // 圆点之间的距离
    _indicatorView.radius = 8; // 圆点半径
    //    _indicatorView.delay = 0;
    
    [self.view addSubview:_indicatorView];
    [_indicatorView startAnimating];
}

#pragma mark - 加载等待动画的代理方法
- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView circleBackgroundColorAtIndex:(NSUInteger)index
{
    switch (index) {
        case 0:
            return [UIColor redColor];
            break;
        case 1:
            return [UIColor magentaColor];
            break;
        case 2:
            return [UIColor orangeColor];
            break;
        case 3:
            return [UIColor purpleColor];
            break;
        default:
            return [UIColor yellowColor];
            break;;
    }
}




//下拉放大顶部图片
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat f = scrollView.contentOffset.y;
    
    if (f < -210) {
        CGRect newF = self.titleImage.frame;
        newF.origin.y = f;
        newF.size.height = -f;
        self.titleImage.frame = newF;
    }
}

+ (CGFloat)cellHeightWithModel:(NSString *)str font:(NSInteger)font
{
    // 计算字符串高度
    // 字号一定与lable相等
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName];
    CGRect lableFrame = [str boundingRectWithSize:CGSizeMake(kScreenWidth - 17, 100000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return lableFrame.size.height;
}


- (void)actionRightButton:(UIButton *)collectButton
{
    if (self.model) {
        DestinationScenicModel *newModel = [[DestinationDatabaseManager shareManager] selectModelByName:self.model.name];
        if ([newModel.name isEqualToString:self.model.name]) {
            [[DestinationDatabaseManager shareManager] deleteByName:newModel.name];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"取消收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
            // 创建按钮
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:nil];
            collectButton.selected = NO;
        } else {
            [[DestinationDatabaseManager shareManager] insertDatabaseWithModel:self.model];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
            // 创建按钮
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:nil];
            collectButton.selected = YES;
        }
        
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"亲,你的数据尚未加载完成,请稍后收藏" preferredStyle:(UIAlertControllerStyleAlert)];
        // 创建按钮
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        [alert addAction:okAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
